import DbService from '../dbservice';

class Performer {
    private dbPromise = DbService.getDbInstance();

    public async set(key: string, value: string): Promise<void> {
        const db = await this.dbPromise;
        try {
            await db.execute('REPLACE INTO t_performer (key, value) VALUES (?, ?)', [key, value]);
        } catch (error) {
            console.error(`Error setting value for key "${key}":`, error);
            throw error;
        }
    }

    public async delete(key: string): Promise<void> {
        const db = await this.dbPromise;
        try {
            await db.execute('DELETE FROM t_performer WHERE key = ?', [key]);
        } catch (error) {
            console.error(`Error deleting key "${key}":`, error);
            throw error;
        }
    }

    public async getAll(): Promise<{ key: string; value: string }[]> {
        const db = await this.dbPromise;
        try {
            const result = await db.select<{ key: string; value: string }[]>('SELECT key, value FROM t_performer');
            return result;
        } catch (error) {
            console.error("Error getting all key-value pairs:", error);
            throw error;
        }
    }

    public async get(key: string): Promise<string | null> {
        const db = await this.dbPromise;
        try {
            const result = await db.select<{ value: string }[]>('SELECT value FROM t_performer WHERE key = ?', [key]);
            return result.length > 0 ? result[0].value : null;
        } catch (error) {
            console.error(`Error getting value for key "${key}":`, error);
            throw error;
        }
    }
}

export default Performer;
